package com.nhjf.admin.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.nhjf.admin.common.util.LogMessage;
import com.nhjf.admin.common.util.SysProperties;


public class CrudPicUtil {
	
	String sysSeparator ;  // windows是\，unix是/
	String fileUpload;
	String tmpPath ;// 临时文件目录
	String crudPath; // 商户图片保存的目录地址
	String imageType;
	String upload;
	
	public  CrudPicUtil(){
		  sysSeparator = File.separator; // windows是\，unix是/ 
		  upload= SysProperties.getInstance().get("fileUploadPath")+sysSeparator+"upload"+sysSeparator;
		  fileUpload =SysProperties.getInstance().get("fileUploadPath")+sysSeparator+ "fileUpload" + sysSeparator;
		  tmpPath = fileUpload + "tmp"; // 临时文件目录
		  crudPath = fileUpload + "crud"; // 商户保存的目录地址
		  imageType = ".jpg";
	}
	
	
	public boolean mkdirForCrud(String crudID){
	       boolean flag=true;
	       StringBuffer picFileDir= new StringBuffer();
	        picFileDir.append(crudPath).append(sysSeparator).append(sysSeparator).append(crudID);
         File fileDir = new File(picFileDir.toString());
			if (!fileDir.exists()) {// 存在该目录，则删除
			   flag = fileDir.mkdir();
			}
			return flag;
	}
	
	
	/**
	 * 上传文件到临时文件夹里
	 * @param file       文件
	 * @param FileName   保存图片的路径
	 * @return
	 */
	public String uploadFileToTmp(File file,String FileName){
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(tmpPath).append(sysSeparator).append(FileName).toString();
		File fileObj=new File(imageTmpPath);
		fileObj.getParentFile().mkdirs();
		if(fileObj.exists()){
			fileObj.delete();
		}
	    try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(new File(imageTmpPath)));
			byte[] buffer = new byte[64 * 1024];
			int length;
			while ((length = in.read(buffer, 0, buffer.length)) > 0) {
				ou.write(buffer, 0, length);
			}
			in.close();
			ou.flush();
			ou.close();
		} catch (Exception e) {
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
	
	
	/** 组建保存图片路径 */
	public String getCrudPicFilePath(String crudID, String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(crudPath).append(sysSeparator).append(sysSeparator).append(crudID).append(sysSeparator).append(fileName);
        return picFilePath.toString();
	}
	
	
	/**
	 * 创建访问图片地址
	 * @param crudID
	 * @param picName
	 * @return
	 */
	public String getWebRootCrudPath(String crudID, String picName){
		StringBuffer sb = new StringBuffer();
		sb.append(SysProperties.getInstance().get("imagePath")).append("/").append("fileUpload/").append("crud/").append(crudID+"/").append(picName);
        return sb.toString();
	}
	
	
	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 */
	public boolean deleteFile(String filePath){
		 boolean flag=true;
		try {
			File deleteFile = new File(filePath);
			deleteFile.delete();
		} catch (RuntimeException e) {
			flag=false;
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return flag;
	}
	
	
	/**
	 * 删除文件夹 及下面文件
	 * @param sPath
	 * @return
	 */
	public boolean deleteDirectory(String sPath) {  
	    //如果sPath不以文件分隔符结尾，自动添加文件分隔符  
	    if (!sPath.endsWith(File.separator)) {  
	        sPath = sPath + File.separator;  
	    }  
	    File dirFile = new File(sPath);  
	    //如果dir对应的文件不存在，或者不是一个目录，则退出  
	    if (!dirFile.exists() || !dirFile.isDirectory()) {  
	        return false;  
	    }  
	    boolean flag = true;  
	    //删除文件夹下的所有文件(包括子目录)  
	    File[] files = dirFile.listFiles();  
	    for (int i = 0; i < files.length; i++) {  
	        //删除子文件  
	        if (files[i].isFile()) {  
	            flag = deleteFile(files[i].getAbsolutePath());  
	            if (!flag) {
                    break;
                }
	        } //删除子目录  
	        else {  
	            flag = deleteDirectory(files[i].getAbsolutePath());  
	            if (!flag) {
                    break;
                }
	        }  
	    }  
	    if (!flag) {
            return false;
        }
	    //删除当前目录  
	    if (dirFile.delete()) {  
	        return true;  
	    } else {  
	        return false;  
	    }  
	}  
	
	
	
	
	
	public String getSysSeparator() {
		return sysSeparator;
	}
	public void setSysSeparator(String sysSeparator) {
		this.sysSeparator = sysSeparator;
	}
	public String getFileUpload() {
		return fileUpload;
	}
	public void setFileUpload(String fileUpload) {
		this.fileUpload = fileUpload;
	}
	public String getTmpPath() {
		return tmpPath;
	}
	public void setTmpPath(String tmpPath) {
		this.tmpPath = tmpPath;
	}
	public String getCrudPath() {
		return crudPath;
	}
	public void setCrudPath(String crudPath) {
		this.crudPath = crudPath;
	}
	public String getImageType() {
		return imageType;
	}
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}
	public String getUpload() {
		return upload;
	}
	public void setUpload(String upload) {
		this.upload = upload;
	}
	
	
}
